package com.cxl.demo.controller;

import com.cxl.demo.mapper.BuyHistoryMapper;
import com.cxl.demo.mapper.GoodsMapper;
import com.cxl.demo.service.GoodsService;
import com.cxl.demo.util.StaticValue;
import com.cxl.demo.vo.Goods;
import com.cxl.demo.vo.ReturnData;
import com.cxl.demo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {
    @Autowired
    ReturnData returnData;
    @Autowired
    GoodsService goodsService;
    @Autowired
    User user;

    @PostMapping("AddGoods")
    public ReturnData AddGoods(@RequestBody Goods goods){
        returnData.setData(null);
        goods.setProvideUserId(user.getUserId());
        Integer ok=goodsService.addGoods(goods);
        if(ok==1){
            returnData.setStatus(true);
            returnData.setMsg("商品上架成功");
        }else{
            returnData.setStatus(false);
            returnData.setMsg("商品上架失败");
        }
        return returnData;
    }
    @PostMapping("DelGoods")
    public ReturnData DelGoods(@RequestBody Map<String,Object> map){
        returnData.setData(null);
        Integer ok=goodsService.delGoods((Integer) map.get("goodsId"));
        if(ok==1){
            returnData.setStatus(true);
            returnData.setMsg("商品下架成功");
        }else{
            returnData.setStatus(false);
            returnData.setMsg("商品下架失败");
        }
        return returnData;
    }
    @PostMapping("UpdateGoods")
    public ReturnData UpdateGoods(@RequestBody Goods goods){
        returnData.setData(null);
       Integer ok= goodsService.UpdateGoods(goods);
        if(ok==1){
            returnData.setStatus(true);
            returnData.setMsg("商品修改成功");
        }else{
            returnData.setStatus(false);
            returnData.setMsg("商品修改失败");
        }
        return returnData;
    }
    @PostMapping("AddGoodsNumber")
    public ReturnData AddGoodsNumber(@RequestBody Map<String,Object> map){
        returnData.setData(null);
        Integer ok=goodsService.AddGoodsNumber((Integer) map.get("goodsId"),(Integer) map.get("number"));

        if(ok==1){
            returnData.setStatus(true);
            returnData.setMsg("商品修改成功");
        }else{
            returnData.setStatus(false);
            returnData.setMsg("商品修改失败");
        }
        return returnData;
    }
    @PostMapping("GetGoodsList")
    public ReturnData GetGoodsList(@RequestBody Map<String,Object> map){
        returnData.setData(null);

        map.put("page",((Integer)map.get("currentPage")-1)*(Integer)map.get("pageSize"));
        map.put("size",(Integer)map.get("pageSize"));
        map.put("keyWord","%"+map.get("Name")+"%");

        returnData.setMsg("查询完毕");
        returnData.setStatus(true);
        returnData.setData(goodsService.getGoods(map));
        return returnData;
    }

    @PostMapping("GetMyGoods")
    public ReturnData GetMyGoods(@RequestBody Map<String,Object> map){
        returnData.setData(null);

        returnData.setMsg("查询完毕");
        returnData.setStatus(true);
        map.put("page",((Integer)map.get("currentPage")-1)*(Integer)map.get("pageSize"));
        map.put("size",(Integer)map.get("pageSize"));
        map.put("keyWord",user.getUserId());
        returnData.setData(goodsService.getGoodsByUserId(map));
        return returnData;
    }
    @PostMapping("GetGoods")
    public ReturnData GetGoods(@RequestBody Map<String,Object> map){


        Goods goods=goodsService.getGoodsById((Integer) map.get("goodsId"));
        goods.setGoodsPic(StaticValue.URL+goods.getGoodsPic());
        returnData.setData(goods);
            returnData.setStatus(true);
            returnData.setMsg("查询成功");
        return returnData;
    }

    @PostMapping("GetGoodsVal")
    public ReturnData GetGoodsVal(@RequestBody Map<String,Object> map){
        returnData.setData(goodsService.getGoodsVal(user.getUserId()));
        returnData.setStatus(true);
        returnData.setMsg("查询成功");

        return returnData;
    }
}
